﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using global::TerhelesEloszto.Common;
using global::TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class Task : System.Web.UI.Page
	{

		protected global::TerhelesEloszto.Engine.IEngine engine = global::TerhelesEloszto.Engine.Engine.Instance();
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			if( Request.QueryString["msg"] != null )
			{
				if( Request.QueryString["msg"].Equals( "1" ) ) MessageLabel.Text = "A műveletet sikeresen végrehajtottuk!";
				else if( Request.QueryString["msg"].Equals( "0" ) ) MessageLabel.Text = "Hiba történt a művelet végrehajtása közben";
			}
			try
			{
				loadTable();
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/Error.aspx" );
			}

			Button2.Attributes.Add( "onclick", "javascript:toggle_visibility('Description'); return false;" );

		}
		public void loadTable()
		{


			TableCell tCellHeader1 = new TableCell();
			tCellHeader1.Text = "ID";
			TableRow tRow = new TableRow();
			tRow.Cells.Add( tCellHeader1 );
			TableTask.Rows.Add( tRow );

			TableCell tCellHeader2 = new TableCell();
			tCellHeader2.Text = "Név";

			tRow.Cells.Add( tCellHeader2 );
			TableTask.Rows.Add( tRow );

			TableCell tCellHeader3 = new TableCell();
			tCellHeader3.Text = "Tantárgy";
			tRow.Cells.Add( tCellHeader3 );

			/*TableCell tCellHeader4 = new TableCell();
			tCellHeader4.Text = "OKTATÓ";
			tRow.Cells.Add( tCellHeader4 );*/

			TableCell tCellHeader5 = new TableCell();
			tCellHeader5.Text = "Szakterület";
			tRow.Cells.Add( tCellHeader5 );

			TableCell tCellHeader6 = new TableCell();
			tCellHeader6.Text = "Szemeszter";
			tRow.Cells.Add( tCellHeader6 );

			TableCell tCellHeader7 = new TableCell();
			tCellHeader7.Text = "Időpont";
			tRow.Cells.Add( tCellHeader7 );

			TableCell tCellHeader8 = new TableCell();
			tCellHeader8.Text = "Típus";
			tRow.Cells.Add( tCellHeader8 );

			TableCell tCellHeader9 = new TableCell();
			tCellHeader9.Text = "";
			tRow.Cells.Add( tCellHeader9 );

			TableCell tCellHeader10 = new TableCell();
			tCellHeader10.Text = "";
			tRow.Cells.Add( tCellHeader10 );

			TableCell tCellHeader11 = new TableCell();
			tCellHeader11.Text = "";
			tRow.Cells.Add( tCellHeader11 );

			TableTask.GridLines = GridLines.Both;
			TableTask.BorderStyle = BorderStyle.Groove;
			TableTask.CellPadding = 10;
			tRow.CssClass = "bold";
			TableTask.Rows.Add( tRow );


			int i = 0;
			foreach( ITask actTask in FilterTask( engine.GetTasks() ) )
			{

				TableRow actRow = new TableRow();

				TableCell actc1 = new TableCell();
				actc1.Text = actTask.ID.ToString();
				actRow.Cells.Add( actc1 );

				TableCell actc2 = new TableCell();
				actc2.Text = actTask.Name;
				actRow.Cells.Add( actc2 );


				TableCell actc3 = new TableCell();
				actc3.Text = actTask.RelatedFact.Name;
				actRow.Cells.Add( actc3 );

				/*TableCell actc4 = new TableCell();
				try
				{

					actc4.Text = actTask.RelatedTeacher.Name;

				}
				catch
				{
					actc4.Text = "Nincs oktató";
					actc4.ForeColor = System.Drawing.Color.Red;
				}
				actRow.Cells.Add( actc4 );*/

				i = 0;
				TableCell actc5 = new TableCell();
				foreach( ISkill actSkill in actTask.GetRequiredSkills() )
				{
					if( i > 0 ) actc5.Text = actc5.Text + "<br/>";
					actc5.Text = actc5.Text + actSkill.Name;
					i++;

				}
				actRow.Cells.Add( actc5 );

				TableCell actc6 = new TableCell();
				switch( actTask.Semester.ToString() )
				{
					case "autumn": actc6.Text = "őszi"; break;
					case "spring": actc6.Text = "tavaszi"; break;
					case "both": actc6.Text = "mindkettő"; break;
				}
				actRow.Cells.Add( actc6 );

				i = 0;
				TableCell actc7 = new TableCell();
				if( actTask.Type.HasInterval )
				{
					foreach( IInterval actInt in actTask.GetTimeIntervals() )
					{
						String day = "";
						switch( actInt.Day )
						{
							case DayOfWeek.Monday: day = "H"; break;
							case DayOfWeek.Tuesday: day = "K"; break;
							case DayOfWeek.Wednesday: day = "Sze"; break;
							case DayOfWeek.Thursday: day = "Cs"; break;
							case DayOfWeek.Friday: day = "P"; break;
							case DayOfWeek.Saturday: day = "Szo"; break;
							case DayOfWeek.Sunday: day = "V"; break;
						}

						String partity = actInt.Parity ? "Páratlan" : "Páros";
						if( i > 0 ) actc7.Text = actc7.Text + "<br/>";
						actc7.Text = actc7.Text + partity + " " + day + " " + actInt.BeginHour + "-" + actInt.EndHour;
						actc7.Text = actc7.Text + "(<a href=\"YesNo.aspx?ID=" + actTask.ID + "&Interval=" + i + "&T=intervaldelete&Q=Biztosan törli az időpontot\">Törlés</a>)";
						i++;
					}
				}
				else
				{
					actc7.Text = " ";
				}
				//actc6.Text = actTask.GetTimeIntervals()[0].ID.ToString();
				actRow.Cells.Add( actc7 );

				//TableTask.Font.Name = "Arial";
				//TableTask.Font.Bold = true;

				TableCell actc8 = new TableCell();
				actc8.Text = actTask.Type.Name;
				actRow.Cells.Add( actc8 );

				TableCell actc9 = new TableCell();
				actc9.Text = "<a href=\"ModifyTask.aspx?ID=" + actTask.ID + "\">Módosítás<a>";
				actRow.Cells.Add( actc9 );

				TableCell actc10 = new TableCell();
				actc10.Text = "<a href=\"YesNo.aspx?ID=" + actTask.ID + "&T=taskdelete&Q=Biztosan törli a feladatot\">Eltávolítás</a>";
				actRow.Cells.Add( actc10 );

				if( actTask.Type.HasInterval )
				{
					TableCell actc11 = new TableCell();
					actc11.Text = "<a href=\"AddInterval.aspx?ID=" + actTask.ID + "\">Új időpont<a>";
					actRow.Cells.Add( actc11 );
				}
				else
				{
					TableCell actc11 = new TableCell();
					actc11.Text = " ";
					actRow.Cells.Add( actc11 );
				}
				TableTask.Rows.Add( actRow );

			}
		}

		protected void Button1_Click( object sender, EventArgs e )
		{
			Response.Redirect( "~/Admin/CreateTask.aspx" );
		}

		protected void search_Click( object sender, EventArgs e )
		{
			string url = "~/Admin/Task.aspx";
			bool first = false;

			if( filterNameBox.Text.Trim() != "" )
			{
				if( !first )
				{
					first = true;
					url += "?name=" + filterNameBox.Text.Trim();
				}
				else
				{
					url += "&name=" + filterNameBox.Text.Trim();
				}
			}

			if( filterFactBox.Text.Trim() != "" )
			{
				if( !first )
				{
					first = true;
					url += "?fact=" + filterFactBox.Text.Trim();
				}
				else
				{
					url += "&fact=" + filterFactBox.Text.Trim();
				}
			}


			if( FilterTypeBox.Text.Trim() != "" )
			{
				if( !first )
				{
					first = true;
					url += "?type=" + FilterTypeBox.Text.Trim();
				}
				else
				{
					url += "&type=" + FilterTypeBox.Text.Trim();
				}
			}



			Response.Redirect( url );
		}

		protected void showAll_Click( object sender, EventArgs e )
		{
			Response.Redirect( "~/Admin/Task.aspx" );
		}

		private IList<ITask> FilterTask( IList<ITask> taskList )
		{
			string nameFilter = Request.QueryString["name"];
			string factFilter = Request.QueryString["fact"];
			string typeFilter = Request.QueryString["type"];
			if( nameFilter == null ) nameFilter = "";
			if( factFilter == null ) factFilter = "";
			if( typeFilter == null ) typeFilter = "";
			if( !Page.IsPostBack ) filterNameBox.Text = nameFilter;
			if( !Page.IsPostBack ) filterFactBox.Text = factFilter;
			if( !Page.IsPostBack ) FilterTypeBox.Text = typeFilter;


			return taskList.Where( t =>
				( t.Name.ToUpper().LastIndexOf( nameFilter.ToUpper() ) != -1 ) &&
				( t.RelatedFact.Name.ToUpper().LastIndexOf( factFilter.ToUpper() ) != -1 ) &&
				( t.Type.Name.ToUpper().LastIndexOf( typeFilter.ToUpper() ) != -1 )
			).ToList();
		}

	}
}